package com.youlai.boot.modules.game.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.modules.game.model.query.GameUserInfoQuery;
import com.youlai.boot.modules.game.model.vo.GameUserInfo;
import com.youlai.boot.modules.game.service.GameUserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 游戏用户信息控制器
 */
@Tag(name = "04.用户信息管理")
@RestController
@RequestMapping("/api/v1/game/users")
@RequiredArgsConstructor
@Slf4j
public class GameUserInfoController {

    private final GameUserInfoService gameUserInfoService;

    @Operation(summary = "获取用户信息分页列表")
    @GetMapping("/page")
    public PageResult<GameUserInfo> getUserInfoPage(@ParameterObject GameUserInfoQuery queryParams) {
        log.info("查询用户信息分页列表: {}", queryParams);
        Page<GameUserInfo> result = gameUserInfoService.getUserInfoPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "获取用户信息详情")
    @GetMapping("/{id}")
    public Result<GameUserInfo> getUserInfo(@Parameter(description = "用户ID") @PathVariable Long id) {
        log.info("查询用户信息详情: {}", id);
        GameUserInfo userInfo = gameUserInfoService.getUserInfo(id);
        return Result.success(userInfo);
    }

    @Operation(summary = "获取用户游戏类型分布")
    @GetMapping("/game-type-distribution")
    public Result<List<Map<String, Object>>> getGameTypeDistribution(
            @Parameter(description = "用户ID") @RequestParam(required = false) Long userId) {
        log.info("查询用户游戏类型分布: userId={}", userId);
        List<Map<String, Object>> data = gameUserInfoService.getGameTypeDistribution(userId);
        return Result.success(data);
    }

    @Operation(summary = "获取用户活跃度分布")
    @GetMapping("/activity-distribution")
    public Result<List<Map<String, Object>>> getActivityDistribution() {
        log.info("查询用户活跃度分布");
        List<Map<String, Object>> data = gameUserInfoService.getActivityDistribution();
        return Result.success(data);
    }

    @Operation(summary = "获取每月新增用户趋势")
    @GetMapping("/monthly-trend")
    public Result<List<Map<String, Object>>> getMonthlyTrend(
            @Parameter(description = "月数") @RequestParam(defaultValue = "12") Integer months) {
        log.info("查询每月新增用户趋势: months={}", months);
        List<Map<String, Object>> data = gameUserInfoService.getMonthlyTrend(months);
        return Result.success(data);
    }

    @Operation(summary = "获取用户游戏时长分布")
    @GetMapping("/play-time-distribution")
    public Result<List<Map<String, Object>>> getPlayTimeDistribution() {
        log.info("查询用户游戏时长分布");
        List<Map<String, Object>> data = gameUserInfoService.getPlayTimeDistribution();
        return Result.success(data);
    }

    @Operation(summary = "导出用户数据")
    @GetMapping("/export")
    public Result<String> exportUserData(GameUserInfoQuery queryParams) {
        log.info("导出用户数据: {}", queryParams);
        String fileUrl = gameUserInfoService.exportUserData(queryParams);
        return Result.success(fileUrl);
    }

    @Operation(summary = "导出分析报告")
    @GetMapping("/export/analysis")
    public Result<String> exportAnalysisReport() {
        log.info("导出分析报告");
        String fileUrl = gameUserInfoService.exportAnalysisReport();
        return Result.success(fileUrl);
    }
} 